﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Text.RegularExpressions;


public partial class Manage_ListFiles : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        FetchFiles();
    }

    private void FetchFiles()
    {
        string root = Server.MapPath("~/html");
        DirectoryInfo di = new DirectoryInfo(root);
        var q = from aF in di.GetFiles("*.aspx")
                orderby aF.CreationTime.Ticks descending
                select new { Name = aF.Name, Title = FetchFileDescription(aF.FullName), Created = string.Format("{0:yyyy年MM月dd日 HH:mm:ss}", aF.CreationTime) };

        FileView.DataSource = q;
        FileView.DataBind();
    }

    private string FetchFileDescription(string filename)
    {
        FileInfo fi = new FileInfo(filename);
        StreamReader sr = fi.OpenText();
        string firstLine = sr.ReadLine();
        sr.Close();

        //正则表达式样式：<%@ Page Title=""
        int pos1 = firstLine.IndexOf('"') + 1;
        int pos2 = firstLine.IndexOf('"', pos1);

        return firstLine.Substring(pos1, pos2 - pos1);
    }

    protected void Command_DeleteArticle(Object sender, CommandEventArgs e)
    {
        string name = e.CommandArgument.ToString();
        string file = Server.MapPath("~/html/" + name);

        if (File.Exists(file))
        {
            File.Delete(file);
        }
    }
    protected void UpFileButton_Click(object sender, EventArgs e)
    {
        if (UpFileControl.HasFile)
        {
            string name = UpFileControl.FileName;
            string dst = Server.MapPath("~/html/" + name);

            UpFileControl.PostedFile.SaveAs(dst);

            FetchFiles();
        }
    }
}